package com.ruoyi.framework.web.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.framework.web.domain.Serial1DTO;
import com.ruoyi.framework.web.domain.SerialResultDTO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/serial")
public class SerialController extends BaseController{

    @PostMapping("/analysis")
    @ResponseBody
    public R analysis(MultipartFile file) throws Exception {
        CsvReader reader = CsvUtil.getReader();
        List<Serial1DTO> data = processCsv();//reader.read(ResourceUtil.getUtf8Reader("C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_3222432227012\\FileStorage\\File\\2024-06\\PE数据(1)\\PE数据\\手工导出原始数据点\\20240622-ppa-online-1\\1\\33-350.csv"),Serial1DTO.class);
        data = data.stream().sorted(Comparator.comparing(Serial1DTO::getTime)).collect(Collectors.toList());
        List<String> xs = data.stream().map(Serial1DTO::getTime).collect(Collectors.toList());
        List<String> ys1 = data.stream().map(Serial1DTO::getMass1).collect(Collectors.toList());
        List<String> ys2 = data.stream().map(Serial1DTO::getMass1).collect(Collectors.toList());
        List<String> ys3 = data.stream().map(Serial1DTO::getMass1).collect(Collectors.toList());
        xs = new ArrayList<>();
        xs.add("3.265");
        xs.add("5.399");
        xs.add("6.550");
        xs.add("7.638");
        xs.add("7.863");
        xs.add("7,886");
        ys1 = new ArrayList<>();
        ys1.add("57.2");
        ys1.add("56.3");
        ys1.add("59.2");
        ys1.add("33.4");
        ys1.add("31.7");
        ys1.add("41.8");
        ys1.add("40.8");
        return R.ok(data);//SerialResultDTO.builder().xs(xs).ys1(ys1).ys2(ys2).ys3(ys3).build()
    }



    public File multipartFileToFile(MultipartFile file) throws IOException {
        File convFile = null;
        if (file.getContentType() != "image/jpeg") {
            throw new RuntimeException("Sorry, only JPEGs are allowed");
        }
        InputStream input = file.getInputStream();
        convFile = new File(file.getOriginalFilename());
        convFile.createNewFile();
        FileOutputStream output = new FileOutputStream(convFile);
        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        while ((bytesRead = input.read(buffer, 0, 1024)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
        output.close();
        input.close();
        return convFile;
    }

    public static List<Serial1DTO> processCsv() throws Exception {
        List<Serial1DTO> list = Lists.newArrayList();
        CSVReader reader = new CSVReader(new FileReader("C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_3222432227012\\FileStorage\\File\\2024-06\\PE数据(1)\\PE数据\\手工导出原始数据点\\20240622-ppa-online-1\\1\\33-350.csv"));
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            String time = nextLine[0];
            String mass1 = nextLine[1];
            String mass2 = nextLine[2];
            String mass3 = nextLine[3];

            Serial1DTO serial1DTO = Serial1DTO.builder()
                    .time(time)
                    .mass1(mass1)
                    .mass2(mass2)
                    .mass3(mass3)
                    .build();
            list.add(serial1DTO);
        }
        return list;
    }

    public static void main(String[] args) {
        try {
            CSVReader reader = new CSVReader(new FileReader("C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_3222432227012\\FileStorage\\File\\2024-06\\PE数据(1)\\PE数据\\手工导出原始数据点\\20240622-ppa-online-1\\1\\33-350.csv"));
            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                String time = nextLine[0];
                String mass1 = nextLine[1];
                String mass2 = nextLine[2];
                String mass3 = nextLine[3];

                Serial1DTO student = Serial1DTO.builder()
                        .time(time)
                        .mass1(mass1)
                        .mass2(mass2)
                        .mass3(mass3)
                        .build();
                System.out.println(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
